package reserve
import (
    //"time"
	"strings"
	"errors"
	"dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//预约周期模板
type PeriodStl struct {
	PeriodSTLId int64       `orm:"column(period_stl_id);pk;auto" json:"period_stl_id" form:"period_stl_id"`
	PartnerId   int64         `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`   //商户ID
	SchemaId    int64       `orm:"column(schema_id)" json:"schema_id" form:"schema_id"`      //商家公共模板:0;项目私有模板:非0   
	Name        string      `orm:"column(name);size(128);null" json:"name" form:"name"`      //名称,比如:周一、周二...
	EnName      string      `orm:"column(en_name);size(128);null" json:"-" form:"-"`         //英文名称
	PeriodNo    int         `orm:"column(period_no)" json:"period_no" form:"period_no"`      //周期序号
	Status      int         `orm:"column(status);default(1)" json:"status" form:"status"`    //是否有效: 1 有效; 0 无效
}

func init() {
	orm.RegisterModel(new(PeriodStl))
}

func (u *PeriodStl) TableUnique() [][]string {
	return [][]string{
		[]string{"PartnerId", "SchemaId", "Name"},
	}
}

func InitDefaultPeriodSTLs(partnerId int64) (err error) {
    periodSTLRes := GetPeriodSTLResource(partnerId)
    err = AddMultiPeriodSTLs(periodSTLRes)
	return
}

//系统周期公有模板
func GetPeriodSTLResource(partnerId int64) (resources []*PeriodStl) {
	resources = []*PeriodStl{}
	//周一
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name:  "周一",
		EnName: "Monday",
		PeriodNo: 1,
		Status: 1,
	})
    //周二
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name: "周二",
		EnName: "Tuesday",
		PeriodNo: 2,
		Status: 1,
	})
	//周三
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name: "周三",
		EnName: "Wednesday",
		PeriodNo: 3,
		Status: 1,
	})
	//周四
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name: "周四",
		EnName: "Thursday",
		PeriodNo: 4,
		Status: 1,
	})
    //周五
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name: "周五",
		EnName: "Friday",
		PeriodNo: 5,
		Status: 1,
	})
    //周六
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name: "周六",
		EnName: "Saturday",
		PeriodNo: 6,
		Status: 1,
	})
    //周日
	resources = append(resources, &PeriodStl{
		PartnerId: partnerId,
		SchemaId:  0,
		Name: "周日",
		EnName: "Sunday",
		PeriodNo: 7,
		Status: 1,
	})
	return resources
}

//复制公有模板到项目私有模板
func CopyPeriodSTL(partnerId,schemaId int64,srcPeriodStl []*PeriodStl) (dstPeriodStl []*PeriodStl) {
	dstPeriodStl = []*PeriodStl{}
	for _, value := range srcPeriodStl {
	    dstPeriodStl = append(dstPeriodStl, &PeriodStl{
			PartnerId: partnerId,
			SchemaId:  schemaId,
			Name:      value.Name,
			EnName:    value.EnName,
			PeriodNo:  value.PeriodNo,
			Status:    value.Status,
		})
	}
	return dstPeriodStl
}

func AddPeriodSTL(m *PeriodStl) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func AddMultiPeriodSTLs(list []*PeriodStl) (err error) {
	size := len(list)
	o := models.GetDefaultDB()
	if size > 1 {
	    _, err = o.InsertMulti(size, list) 
	}else{
	   _, err = o.Insert(list[0])
	}
	return
}

func GetPeriodSTLById(id int64) (v *PeriodStl, err error) {
	o := models.GetDefaultDB()
	v = &PeriodStl{PeriodSTLId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllPeriodSTLs(query,order_by map[string]string)(lists []*PeriodStl, err error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(PeriodStl))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	if _, err = qs.All(&lists); err == nil {
		return
	}
	return
}
func UpdatePeriodSTLById(m *PeriodStl, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := PeriodStl{PeriodSTLId: m.PeriodSTLId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}
func DeletePeriodSTLById(id int64) (err error) {
	o := models.GetDefaultDB()
	v := PeriodStl{PeriodSTLId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

func DeletePeriodSTLsByPartnerId(partnerId int64) (err error) {
    o := models.GetDefaultDB()
	_, err = o.QueryTable(new(PeriodStl)).Filter("partner_id", partnerId).Delete()
	return
}
